package com.cmict.data.system.api.dept;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.system.api.sql.SqlParameterCheck;
import com.cmict.data.framework.datapermission.core.annotation.DataPermission;
import com.cmict.data.system.api.dept.dto.DeptRespDTO;
import com.cmict.data.system.controller.admin.dept.vo.dept.DeptListReqVO;
import com.cmict.data.system.convert.dept.DeptConvert;
import com.cmict.data.system.enums.ApiConstants;
import com.cmict.data.system.service.dept.DeptService;
import com.cmict.data.system.dal.dataobject.dept.DeptDO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = ApiConstants.VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
@DataPermission(enable = false) //忽略数据权限
public class DeptApiImpl implements DeptApi {

    @Resource
    private DeptService deptService;

    @Override
    public CommonResult<DeptRespDTO> getDept(Long id) {
        DeptDO dept = deptService.getDept(id);
        return success(DeptConvert.INSTANCE.convert03(dept));
    }

    @Override
    public CommonResult<List<DeptRespDTO>> getDeptsByParentId(Long id) {
        List<DeptDO> depts = deptService.getDeptsByParentId(id);
        return success(DeptConvert.INSTANCE.convertList03(depts));
    }

    @Override
    public CommonResult<List<Long>> getAllId() {
        return success(deptService.selectAllId());
    }

    @Override
    public CommonResult<List<DeptRespDTO>> getDeptsByIdsJoin(String ids) {
        List<DeptDO> depts = deptService.getDepts(Arrays.stream(ids.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()));
        return success(DeptConvert.INSTANCE.convertList03(depts));
    }


    @Override
    public CommonResult<List<DeptRespDTO>> getDepts(Collection<Long> ids) {
        List<DeptDO> depts = deptService.getDepts(ids);
        return success(DeptConvert.INSTANCE.convertList03(depts));
    }

    @Override
    public CommonResult<Boolean> validDepts(Collection<Long> ids) {
        deptService.validDepts(ids);
        return success(true);
    }

    @Override
    public CommonResult<DeptRespDTO> getDeptByName(String name) {
        DeptDO dept = deptService.getDept(name);
        return success(DeptConvert.INSTANCE.convert03(dept));
    }

    @Override
    public CommonResult<List<DeptRespDTO>> getDeptsByIds(Collection<Long> ids) {
        ids.stream().forEach(id -> SqlParameterCheck.checkDigitalMatch(String.valueOf(id)));
        List<DeptDO> depts = deptService.getDeptsByIds(ids);
        return success(DeptConvert.INSTANCE.convertList03(depts));
    }

    @Override
    public CommonResult<List<DeptRespDTO>> selectListAll() {
        List<DeptRespDTO> deptRespDTOS = DeptConvert.INSTANCE.convertList04(deptService.selectListAll());
        return success(deptRespDTOS);
    }

    @Override
    public CommonResult<List<DeptRespDTO>> selectListAllOpen() {
        DeptListReqVO reqVO = new DeptListReqVO();
        reqVO.setStatus(0);
        List<DeptRespDTO> deptRespDTOS = DeptConvert.INSTANCE.convertList04(deptService.getSimpleDepts(reqVO));
        return success(deptRespDTOS);
    }
}
